import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'


// 布局组件
import MainLayout from '../layouts/mainLayout.vue'
import AuthLayout from '../layouts/authLayout.vue'
import PlanLayout from '../layouts/planLayout.vue'
import BookLayout from '../layouts/bookLayout.vue'
import ExamLayout from '../layouts/examLayout.vue'
import interaction from '../layouts/interactionLayout.vue'
import answerLayout from '../layouts/answerLayout.vue'
import MyLayout from '../layouts/myLayout.vue'

import Home from '../views/home/home.vue'
import Login from '../views/auth/login/login.vue'
import My from '../views/my/my.vue'
import TodayPlan from '../views/plan/todayPlan.vue'
import allPlan from '../views/plan/allPlan.vue'
import incompletePlan from '../views/plan/incompletePlan.vue'

import Book from '../views/book/book.vue'
import Law from '../views/book/law.vue'
import Mindmap from '../views/book/mindmap.vue'
import Outline from '../views/book/outline.vue'
import Video from '../views/book/video.vue'
import LawDetail from '../views/book/lawDetail.vue'
import VideoDetail from '../views/book/videoDetail.vue'
import PointExam from '../views/exam/pointExam.vue'
import TrueExam from '../views/exam/trueExam.vue'
import SimulatedExam from '../views/exam/simulatedExam.vue'
import SmartExam from '../views/exam/smartExam.vue'
import WrongExam from '../views/exam/wrongExam.vue'
import TrueExamDetail from '../views/exam/trueExamDetail.vue'
import SimulatedExamDetail from '../views/exam/simulatedExamDetail.vue'
import PointExamDetail from '../views/exam/pointExamDetail.vue'
import WrongExamDetail from '../views/exam/wrongExamDetail.vue'
import Classmate from '../views/interaction/classmate.vue'
import Race from '../views/interaction/race.vue'
import InteractionLayout from '../layouts/interactionLayout.vue'
import ClassmateDetail from '../views/interaction/classmateDetail.vue'
import RaceDetail from '../views/interaction/raceDetail.vue'
import Ai from '../views/ai/ai.vue'
import AiDo from '../views/ai/aiDo.vue'



const routes = [
  {
    path:'/',
    redirect: '/home'
  },
  {
    path: '/',
    component: MainLayout,
    //redirect: '/home', // 默认重定向到登录页
    children: [
      {
        path: 'home',
        name: 'Home',
        component: Home
      },
      {
        path: '/book',
        name: 'Book',
        redirect: '/book/outline',
        component: BookLayout,
        children:[
          {
            path: 'book/:type',
            name: 'BookIndex',
            component: Book
          },
          {
            path: 'law',
            name: 'law',
            component: Law,
            meta: {
              tabIdentifier: 'law' // 保持相同标识
            }
          },
          {
            path: 'law/lawDetail',
            name: 'lawDetail',
            component: LawDetail,
            meta: {
              tabIdentifier: 'law' // 保持相同标识
            }
          } ,
          {
            path: 'mindmap',
            name: 'mindmap',
            component: Mindmap
          },
          {
            path: 'outline',
            name: 'outline',
            component: Outline
          },
          {
            path: 'video',
            name: 'video',
            component: Video
          },
          {
            path: 'video/videoDetail',
            name: 'videoDetail',
            component: VideoDetail
          }
        ]
      },
      {
        path: '/plan',
        name: 'TodayPlan',
        redirect: '/plan/todayPlan',
        component: PlanLayout,
        children:[
          {
            path: 'todayPlan',
            name: 'TodayPlanIndex',
            component: TodayPlan
          },
          {
            path: 'incompletePlan',
            name: 'IncompletePlan',
            component: incompletePlan
          },
          {
            path: 'allPlan',
            name: 'AllPlan',
            component: allPlan
          }
        ]
      },
      {
        path: '/exam',
        name: 'Exam',
        redirect: '/exam/trueExam',
        component: ExamLayout,
        children:[
          {
            path: 'trueExam',
            name: 'TrueExam',
            component: TrueExam,
            meta: {
              tabIdentifier: 'trueExam' // 保持相同标识
            }
          },
          {
            path:'trueExam/trueExamDetail',
            name:'TrueExamDetail',
            component:TrueExamDetail,
            meta: {
              tabIdentifier: 'trueExam' // 保持相同标识
            }
          },
          {
            path: 'simulatedExam',
            name: 'SimulatedExam',
            component: SimulatedExam
          },
          {
            path:'simulatedExam/simulatedExamDetail',
            name:'SimulatedExamDetail',
            component:SimulatedExamDetail,
            meta: {
              tabIdentifier: 'simulatedExam' // 保持相同标识
            }
          },
          {
            path: 'smartExam',
            name: 'SmartExam',
            component: SmartExam
          },
          {
            path: 'wrongExam',
            name: 'WrongExam',
            component: WrongExam
          },
          {
            path: 'wrongExam/wrongExamDetail',
            name: 'WrongExamDetail',
            component: WrongExamDetail,
            meta: {
              tabIdentifier: 'wrongExam' // 保持相同标识
            }
          },
          {
            path: 'pointExam',
            name: 'PointExam',
            component: PointExam
          },
          {
            path: 'pointExam/pointExamDetail',
            name: 'PointExamDetail',
            component: PointExamDetail,
            meta: {
              tabIdentifier: 'pointExam' // 保持相同标识
            }
          }
        ]
      },
      {
        path: '/interaction',
        name: 'Interaction',
        redirect: '/interaction/classmate',
        component: InteractionLayout,
        children:[
          {
            path: 'classmate',
            name: 'Classmate',
            component: Classmate
          },{
            path: 'classmate/classmateDetail',
            name: 'ClassmateDetail',
            component: ClassmateDetail,
            meta: {
              tabIdentifier: 'classmate' // 保持相同标识
            }
          },
          {
            path: 'race',
            name: 'Race',
            component: Race
          },
          {
            path: 'race/raceDetail',
            name: 'RaceDetail',
            component: RaceDetail,
            meta: {
              tabIdentifier: 'race' // 保持相同标识
            }
          }
        ]
      },
      {
        path: '/ai',
        name: 'Ai',
        component: Ai
      },
      {
        path: '/aiDo',
        name: 'AiDo',
        component: AiDo
      },
      {
        path: '/ai/callback',
        name: 'AiCallback',
        component: Ai
      },
      {
        path: '/my',
        name: 'My',
        component: My
      }
    ]
    
  },
  {
    path: '/auth',
    component: AuthLayout,
    children:[
      {
        path: '/login',
        name: 'Login',
        component: Login
      }
    ]
  }
]



const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加全局路由守卫
router.beforeEach((to, from, next) => {
  let userInfo = JSON.parse(localStorage.getItem("userInfo"))
  let isAuthenticated = false
  if(userInfo && userInfo.token != ""){
    isAuthenticated = true
  }
 
  
  if (to.name !== 'Login' && !isAuthenticated) {
    next({ name: 'Login' })
  } else {
    next()
  }
})

export default router